package net.ideapad.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ideapad.data.Group;
import net.ideapad.data.User;
import net.ideapad.hibernate.HibernateSessionFactory;
import net.ideapad.struts.form.ManageGroupForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;

/**
 * Action class for updating the properties of a group.
 * 
 * MyEclipse Struts Creation date: 12-16-2009
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/manageGroup" name="manageGroupForm"
 *                input="/form/manageGroup.jsp" scope="request" validate="true"
 */
public class ManageGroupAction extends Action {
	/**
	 * Method that takes the new Group infromation from the form and updates the
	 * group in the database.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		ManageGroupForm manageGroupForm = (ManageGroupForm) form;

		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();

		// If nothing is updated, no update request is sent to the DB.
		boolean isChange = false;

		// Fetch the group being modified
		Group g = (Group) session.createQuery("select g from Group g left join fetch g.moderators left join fetch g.usersInGroup where g.groupId = :id").setInteger("id", manageGroupForm.getGroupID()).uniqueResult();

		// Update the fields of the group that have changed.
		if (!manageGroupForm.getGroupName().equals("")) {
			g.setGroupName(manageGroupForm.getGroupName());
			isChange = true;
		}
		if (manageGroupForm.getRemoveUserID() != null) {
			User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", manageGroupForm.getRemoveUserID()).uniqueResult();
			g.getUsersInGroup().remove(u);
			isChange = true;
		}
		if (manageGroupForm.getRemoveModeratorID() != null) {
			User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", manageGroupForm.getRemoveModeratorID()).uniqueResult();
			g.getModerators().remove(u);
			g.getUsersInGroup().remove(u);
			isChange = true;
		}
		if (manageGroupForm.getAddUserID() != null) {
			User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", manageGroupForm.getAddUserID()).uniqueResult();
			g.getUsersInGroup().add(u);
			isChange = true;
		}
		if (manageGroupForm.getAddModeratorID() != null) {
			User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", manageGroupForm.getAddModeratorID()).uniqueResult();
			g.getModerators().add(u);
			g.getUsersInGroup().add(u);
			isChange = true;
		}

		if (isChange) {
			session.saveOrUpdate(g);
		}

		session.getTransaction().commit();
		HibernateSessionFactory.closeSession();

		// Return to the Manage Groups page.
		return (new ManageGroupsAction()).execute(mapping, null, request, response);
	}
}